package win.zxshuo.www.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import win.zxshuo.www.model.BookCrawlerModel;
import win.zxshuo.www.model.BookModel;

import java.util.List;

/**
 * Author:rffanlab
 * Created:2018/4/23
 */
@Repository
public interface BookCrawlerRepository extends JpaRepository<BookCrawlerModel,Long> {

    /**
     * 查询1000个需要更新的书籍（包括已经完本但未采集的，和未完本的。）
     * @return
     */
    @Query(value = "select b.* from book b left join bookcrawler bc on b.id=bc.bookid where (bc.id is null) or (bc.isFinished!=1 and hour(timediff(now(),lastUpdated))>6) or (bc.isFinished =1  and bc.finishedBookFinishedCrawler = 0 ) limit 1000",nativeQuery = true)
    public List<BookModel> getAllBooksNeedToUpdateLimit1000();





    /**
     * 通过书籍id来获取采集记录
     * @param bookid
     * @return
     */
    @Query(value = "select * from bookcrawler where bookid=?1",nativeQuery = true)
    public BookCrawlerModel getByBookid(Long bookid);



}
